package com.bjtime.designer


import android.content.Context
import android.content.DialogInterface
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix
import android.graphics.drawable.BitmapDrawable
import android.util.Log
import android.widget.Toast
import com.bjtime.designer.attributes.*
import com.bjtime.designer.widget.*
import org.json.JSONArray
import org.json.JSONObject
import java.io.ByteArrayOutputStream
import java.lang.Exception


class DataHelper {

    /**
     * 标签JSON
     *  创建日期
     *  修改日期
     *  标签作者
     *  {......}
     */


    companion object {

        fun generateJson (layout:DesignerLayout, w:Int, h:Int, context:Context, pName:String = "ProjectName1") : Boolean {
            if (layout.getAllLabel().size == 0) {
                Toast.makeText(context, "没有任何元素可以保存,GUN", Toast.LENGTH_SHORT).show()
                return false
            }

            var json1 : JSONObject = JSONObject()
            var json2 : JSONObject = JSONObject()
            var jsonArray1 : JSONArray = JSONArray()

            for (b in layout.getAllLabel()) {
                when (b.des_viewtype) {
                    ViewAttr.VIEW_TYPE.LINE->       json2 = _LineView       (b as LineAttr)
                    ViewAttr.VIEW_TYPE.IMAGE->      json2 = _ImageView      (b as ImageAttr)
                    ViewAttr.VIEW_TYPE.QRCODE->     json2 = _QRCodeView     (b as QRCodeAttr)
                    ViewAttr.VIEW_TYPE.BARCODE->    json2 = _BarCodeView    (b as BarCodeAttr)
                    ViewAttr.VIEW_TYPE.TEXT->       json2 = _TextView       (b as TextAttr)
                    ViewAttr.VIEW_TYPE.RECT->       {}
                    ViewAttr.VIEW_TYPE.TABLE->      {}
                    ViewAttr.VIEW_TYPE.BASE->       {}
                }
                jsonArray1.put(json2)
            }


            json1.put("ProjectName",pName)
            json1.put("CreateData",System.currentTimeMillis())
            json1.put("UpdateData",System.currentTimeMillis())
            json1.put("LabelAuthor","北京时间零点")
            // 增加一个des_LabelWidth/ Height, 这是在手机上的设计尺寸, 用于在不同尺寸的屏幕上作为缩放凭据.
            json1.put("des_LabelWidht",context.resources.displayMetrics.widthPixels)
            json1.put("des_LabelHeight",context.resources.displayMetrics.heightPixels)
            json1.put("LabelWidht",w)
            json1.put("LabelHeight",h)
            json1.put("speed",h)        // 打印速度
            json1.put("density",h)      // 打印浓度
            json1.put("direction",h)    // 打印方向
            json1.put("spac",h)         // 纸张间隔
            json1.put("type",h)         // 纸张类型



            var b = layout.shot()
            if (b != null) {
                var os : ByteArrayOutputStream = ByteArrayOutputStream ()
                b.compress(Bitmap.CompressFormat.JPEG, 100, os)
                os.flush()
                os.close()

                var bs = Jutils.encode2String(os.toByteArray())
                json1.put("Preview", bs)
            }
            json1.put("labels", jsonArray1)


            try {
                if (!Jutils.nativeSaveHpiFile("/sdcard/fuck.hpi", json1.toString()))
                    return false
                return true
            } catch (e : Exception) {
                e.printStackTrace()
                // 基本上就是无权限.
                return false
            }
        }


        private fun __getBaseAttr (attr : BaseAttr) : JSONObject {
            var json1 = JSONObject ()
            json1.put("des_id", attr.des_id)
            json1.put("des_ptype", attr.des_ptype)
            json1.put("des_reversed", attr.des_reversed)
            json1.put("des_viewtype", attr.des_viewtype)
            json1.put("des_left", attr.des_left)
            json1.put("des_top", attr.des_top)
            json1.put("des_width", attr.des_width)
            json1.put("des_height", attr.des_height)
            json1.put("des_lock", attr.des_lock)
            json1.put("des_rotate", attr.des_rotate)
            return json1
        }

        private fun _LineView (attr : LineAttr) : JSONObject {
            var json1 = __getBaseAttr (attr)
            json1.put("des_line_style", attr.des_line_style)
            json1.put("prt_left", attr.prt_left)
            json1.put("prt_top", attr.prt_top)
            json1.put("prt_width", attr.prt_right)
            json1.put("prt_height", attr.prt_bottom)
            return json1
        }

        private fun _ImageView (attr : ImageAttr) : JSONObject {
            var json1 = __getBaseAttr (attr)
            json1.put("des_image", Jutils.encode2String(attr.des_image!!))
            json1.put("des_scale", attr.des_scale)
            json1.put("prt_left", attr.prt_left)
            json1.put("prt_top", attr.prt_top)
            json1.put("prt_width", attr.prt_right)
            json1.put("prt_height", attr.prt_bottom)
            return json1
        }

        private fun _BarCodeView (attr : BarCodeAttr) : JSONObject {
            var json1 = __getBaseAttr (attr)
            json1.put("des_image", Jutils.encode2String(attr.des_image!!))
            json1.put("des_scale", attr.des_scale)
            json1.put("des_text", attr.des_text)
            json1.put("prt_left", attr.prt_left)
            json1.put("prt_top", attr.prt_top)
            json1.put("prt_width", attr.prt_right)
            json1.put("prt_height", attr.prt_bottom)
            return json1
        }

        private fun _QRCodeView (attr : QRCodeAttr) : JSONObject {
            var json1 = __getBaseAttr (attr)
            json1.put("des_image", Jutils.encode2String(attr.des_image!!))
            json1.put("des_scale", attr.des_scale)
            json1.put("des_text", attr.des_text)
            json1.put("prt_left", attr.prt_left)
            json1.put("prt_top", attr.prt_top)
            json1.put("prt_width", attr.prt_right)
            json1.put("prt_height", attr.prt_bottom)
            json1.put("des_type", attr.des_type)
            return json1
        }

        private fun _TextView (attr : TextAttr) : JSONObject {
            var json1 = __getBaseAttr (attr)
            json1.put("des_text", attr.des_text)
            json1.put("des_textsize", attr.des_textsize)

            json1.put("prt_left", attr.prt_left)
            json1.put("prt_top", attr.prt_top)
            json1.put("prt_width", attr.prt_right)
            json1.put("prt_height", attr.prt_bottom)

            json1.put("des_text_bold", attr.des_text_bold)
            json1.put("des_text_italics", attr.des_text_italics)
            json1.put("des_text_underline", attr.des_text_underline)
            json1.put("des_text_deleteline", attr.des_text_deleteline)
            return json1
        }

        // 根据属性,实例化标签,并且添加到设计器中.
        fun generateForJson (s:String, desginer:DesignerLayout) {
            var json1 : JSONObject = JSONObject(s)
            var ars1 = json1.getJSONArray("labels")
            for (i in 0..ars1.length()-1) {
                try {
                    when (ViewAttr.VIEW_TYPE.valueOf(ars1.getJSONObject(i).getString("des_viewtype"))) {
                        ViewAttr.VIEW_TYPE.LINE->       _generateLine  (ars1.getJSONObject(i), desginer);
                        ViewAttr.VIEW_TYPE.IMAGE->      _generateImage (ars1.getJSONObject(i), desginer);
                        ViewAttr.VIEW_TYPE.QRCODE->     _generateQRCode (ars1.getJSONObject(i), desginer);
                        ViewAttr.VIEW_TYPE.BARCODE->    _generateBarCode (ars1.getJSONObject(i), desginer);
                        ViewAttr.VIEW_TYPE.TEXT->       _generateText (ars1.getJSONObject(i), desginer);
                        ViewAttr.VIEW_TYPE.RECT->       {}
                        ViewAttr.VIEW_TYPE.TABLE->      {}
                        ViewAttr.VIEW_TYPE.BASE->       {}
                    }

                } catch (e : Exception) {
                    var ad = android.app.AlertDialog.Builder (desginer.context).create ()
                    ad.setTitle("错误！")
                    ad.setMessage("解析遇到错误：" + e.message)
                    ad.setButton("服你了", DialogInterface.OnClickListener({ a, o ->
                        a.dismiss()
                    }))
                    ad.show()
                }
            }
        }


        fun _generateLine (json : JSONObject, layout:DesignerLayout) {
            var view = LineView2 (layout.context)
            var attr = LineAttr ()
            attr.des_line_style = json.getInt("des_line_style")
            attr.des_id = json.getInt("des_id")
            attr.des_ptype = ViewAttr.PRINT_TYPE.valueOf (json.getString("des_ptype"))
            attr.des_left = json.getInt("des_left")
            attr.des_top = json.getInt("des_top")
            attr.des_width = json.getInt("des_width")
            attr.des_height = json.getInt("des_height")
            attr.des_lock = json.getBoolean("des_lock")
            attr.des_rotate = json.getInt("des_rotate")
            attr.des_reversed = json.getBoolean("des_reversed")
            attr.des_viewtype = ViewAttr.VIEW_TYPE.valueOf(json.getString("des_viewtype"))
            layout.addView(view, attr)
        }

        fun _generateImage (json : JSONObject, layout:DesignerLayout) {
            var view = ImageView2 (layout.context,Jutils.decode(json.getString("des_image").toByteArray()))
            var attr = ImageAttr ()
            attr.des_id = json.getInt("des_id")
            attr.des_ptype = ViewAttr.PRINT_TYPE.valueOf (json.getString("des_ptype"))
            attr.des_left = json.getInt("des_left")
            attr.des_top = json.getInt("des_top")
            attr.des_width = json.getInt("des_width")
            attr.des_height = json.getInt("des_height")
            attr.des_lock = json.getBoolean("des_lock")
            attr.des_rotate = json.getInt("des_rotate")
            attr.des_reversed = json.getBoolean("des_reversed")
            attr.des_viewtype = ViewAttr.VIEW_TYPE.valueOf(json.getString("des_viewtype"))
            attr.des_image = Jutils.decode(json.getString("des_image").toByteArray())
            attr.des_scale = json.getInt("des_scale")
            layout.addView(view, attr)
        }

        fun _generateBarCode (json : JSONObject, layout:DesignerLayout) {
            var view = BarCodeView2 (layout.context,json.getString("des_text"))
            var attr = BarCodeAttr ()
            attr.des_id = json.getInt("des_id")
            attr.des_ptype = ViewAttr.PRINT_TYPE.valueOf (json.getString("des_ptype"))
            attr.des_left = json.getInt("des_left")
            attr.des_top = json.getInt("des_top")
            attr.des_width = json.getInt("des_width")
            attr.des_height = json.getInt("des_height")
            attr.des_lock = json.getBoolean("des_lock")
            attr.des_rotate = json.getInt("des_rotate")
            attr.des_reversed = json.getBoolean("des_reversed")
            attr.des_viewtype = ViewAttr.VIEW_TYPE.valueOf(json.getString("des_viewtype"))
            attr.des_image = Jutils.decode(json.getString("des_image").toByteArray())
            attr.des_scale = json.getInt("des_scale")
            attr.des_text = json.getString("des_text")
            layout.addView(view, attr)
        }

        fun _generateQRCode (json : JSONObject, layout:DesignerLayout) {
            var view = QRCodeView2 (layout.context,json.getString("des_text"))
            var attr = QRCodeAttr ()
            attr.des_id = json.getInt("des_id")
            attr.des_ptype = ViewAttr.PRINT_TYPE.valueOf (json.getString("des_ptype"))
            attr.des_left = json.getInt("des_left")
            attr.des_top = json.getInt("des_top")
            attr.des_width = json.getInt("des_width")
            attr.des_height = json.getInt("des_height")
            attr.des_lock = json.getBoolean("des_lock")
            attr.des_rotate = json.getInt("des_rotate")
            attr.des_reversed = json.getBoolean("des_reversed")
            attr.des_viewtype = ViewAttr.VIEW_TYPE.valueOf(json.getString("des_viewtype"))
            attr.des_image = Jutils.decode(json.getString("des_image").toByteArray())
            attr.des_scale = json.getInt("des_scale")
            attr.des_text = json.getString("des_text")
            attr.des_type = QRCodeAttr.QRTYPE.valueOf(json.getString("des_type"))
            layout.addView(view, attr)
        }

        fun _generateText (json : JSONObject, layout:DesignerLayout) {
            var view = TextView2 (layout.context,json.getString("des_text"))
            var attr = TextAttr ()
            attr.des_id = json.getInt("des_id")
            attr.des_ptype = ViewAttr.PRINT_TYPE.valueOf (json.getString("des_ptype"))
            attr.des_left = json.getInt("des_left")
            attr.des_top = json.getInt("des_top")
            attr.des_width = json.getInt("des_width")
            attr.des_height = json.getInt("des_height")
            attr.des_lock = json.getBoolean("des_lock")
            attr.des_rotate = json.getInt("des_rotate")
            attr.des_reversed = json.getBoolean("des_reversed")
            attr.des_viewtype = ViewAttr.VIEW_TYPE.valueOf(json.getString("des_viewtype"))
            attr.des_text = json.getString("des_text")
            attr.des_textsize = json.getInt("des_textsize")

            attr.des_text_bold = json.getBoolean("des_text_bold")
            attr.des_text_italics = json.getBoolean("des_text_italics")
            attr.des_text_underline = json.getBoolean("des_text_underline")
            attr.des_text_deleteline = json.getBoolean("des_text_deleteline")

            layout.addView(view, attr)
        }


    }
}